#! /usr/bin/perl -wl
#------------------------------------------------
# Author:    vj
# Created:   Fri Sep 28 06:00:51 IST 2012
# File Name: analyzer
# USAGE: 
#       analyzer 
# 
# 
#------------------------------------------------
@allAttrs = qw/sim_num_insn sim_num_refs sim_elapsed_time sim_inst_rate sim_num_branches sim_IPB lookups updates addr_hits dir_hits misses jr_hits jr_seen jr_non_ras_hits.PP jr_non_ras_seen.PP bpred_addr_rate bpred_dir_rate bpred_jr_rate retstack_pushes retstack_pops used_ras.PP ras_hits.PP ras_rate.PP/;

foreach $File ( @ARGV ) {
  open FH, "$File" or die "Opening $File failed due to $!";
  ($modeStr = $File) =~ s#.*/|\.[^\.]*$##g;
  push @keys, $modeStr;
  
  $Flag = 0;
  while (<FH>) {
    chomp;
    $Flag = 1 if (/^sim: \*\* simulation statistics \*\*$/);
    if ($Flag) {
      s/#.*$|^\S*?\.//g;
      if (/^(\S*)\s+(\S*)\s*$/) {
        $DB{$modeStr}{$1} = $2;
        if ( not defined $commAttrs{$1} ) {
          $commAttrs{$1} = $2
        } elsif ( $commAttrs{$1} ne $2 ) {
          $commAttrs{$1} = 'X';
        }
      }
    }
  }
  close FH;
}

foreach ( @allAttrs ) {
  if ($commAttrs{$_} ne 'X') {
    print "$_,$commAttrs{$_}";
  } elsif ($_ =~ /jr/) {
    push @lpattrs, $_;
  } else {
    push @attrs, $_;
  }
}
@lpattrs = @lpattrs;
#push @attrs, @lpattrs;

if (1) {
  print join ",", "Mode\\Attr", @attrs;
  foreach $key ( @keys ) {
    printf "$key";
    foreach $attr ( @attrs ) { if (exists $DB{$key}{$attr}) { printf ",$DB{$key}{$attr}" } else {printf ",_" } }
    printf "\n";
  }
} else {
  print join ",", "Attr\\Mode", keys %DB;
  foreach $attr ( @attrs ) { 
    printf "$attr";
    foreach $key ( @keys ) { if (exists $DB{$key}{$attr}) { printf ",$DB{$key}{$attr}" } else {printf ",_" } }
    printf "\n";
  }
}
